/*
 * Copyright 2010 Groupware.cc All right reserved. This software is the
 * confidential and proprietary information of Groupware.cc ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Groupware.cc.
 */
package com.rome.syncml.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.rome.syncml.bean.BaseVO;
import com.rome.syncml.bean.SMS;
import com.rome.syncml.bean.SMSVO;
import com.rome.syncml.constant.Constant;
import com.rome.syncml.domain.SyncItem;
import com.rome.syncml.persistence.SyncItemMapper;
import com.rome.syncml.util.SMSUtil;

/**
 * 短信 
 * @author afei 2011-11-25 下午10:11:34
 */
@Service
public class SMSService {

    @Resource
    private SyncItemMapper      syncItemMapper;
    
    private int pageSize = 10;
    private final String smsType = "sms";
    /**
     * 
     * @param userId
     * @param queryString
     * @param currentPage
     * @param type 0 全部 1:接受的 2:发送的
     * @return
     */
    public BaseVO querySMS(long userId,String queryString,int currentPage,int type){
        int start = (currentPage-1)*pageSize;
        String condition=null;
        if(type!=Constant.SMS_TYPE_ALL){
            condition = "\"type\":" + type;
        }
        List<SyncItem> items = syncItemMapper.queryItems(userId, 1L, false, smsType, queryString,condition,start,pageSize);
        int count = syncItemMapper.countItems(userId, 1L, false, smsType, queryString,condition);
        SMSVO vo = new SMSVO();
        vo.setCount(count);
        vo.setCurrentPage(currentPage);
        vo.setPageSize(pageSize);
        vo.setTotalPage((count+pageSize-1)/pageSize);
        if(items!=null){
            List<SMS> smss = new ArrayList<SMS>();
            for(SyncItem item : items){
                SMS sms = SMSUtil.parseSms(item.getContent());
                sms.setId(item.getId());
                sms.setUserId(item.getUserId());
                Date date = new Date(sms.getDate());
                sms.setFormatDate(date);
                smss.add(sms);
            }
            vo.setSmss(smss);
        }
        return vo;
    }
    
    public void delete(long[] ids){
        for(long id : ids){
            syncItemMapper.logicDelete(id);
        }
    }
}
